package com.persicadev.khayyam;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import org.xmlpull.v1.XmlPullParserException;

import com.persicadev.khayyam.R;


import android.content.Context;
import android.content.res.XmlResourceParser;

public class PoemEngine implements IPoemDb {

	private Context context;
	public PoemEngine(Context context)
	{
		this.context=context;
	}
	
	public int totalPoems() 
	{
		XmlResourceParser asd = context.getResources().getXml(R.xml.poemdb);
		int poemNo=0;

		try 
		{
			while (asd.next() != XmlResourceParser.END_DOCUMENT ) 
			{
				String att = asd.getAttributeValue(null,"Id");
				if (att != null)
				{
					++poemNo;
				}
			}
		} catch (XmlPullParserException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return poemNo;

	}

	public Poem getPoem(int id) 
    {
    	Verse verse1 = new Verse();
    	Verse verse2 = new Verse();

    	Poem poem = new Poem(PoemType.robaei);

    	XmlResourceParser asd = context.getResources().getXml(R.xml.poemdb);

    	try {


    		boolean currentRobaei = false;
    		String MesraNo ="" ;
    		while (asd.next() != XmlResourceParser.END_DOCUMENT ) {


    			String att = asd.getAttributeValue(null,"Id");

    			int depth = asd.getDepth();

    			if (currentRobaei==true && depth > 3)
    			{

    			}
    			
    			else if(currentRobaei==true && depth <= 3)
    			{
    				break;
    			}


    			if (att != null && att.equals(String.valueOf(id)))
    			{
    				currentRobaei = true;
    			}
    			else if(currentRobaei==false)
    			{
    				continue;
    			}


    			String s = asd.getText();

    			if	(MesraNo != null)
    			{

    				if (s != null && MesraNo.equals("1") ) 
    				{
    					verse1.setPart1(s);
    				}
    				else if (s!=null && MesraNo.equals("2")) 
    				{
    					verse1.setPart2(s);        			
    				} 
    				else if (s!=null && MesraNo.equals("3")) 
    				{
    					verse2.setPart1(s);        			
    				} 
    				else if (s!=null && MesraNo.equals("4")) 
    				{
    					verse2.setPart2(s);        			
    				} 

    			}
    			MesraNo = asd.getAttributeValue(null, "No");
    		}
    	} catch (XmlPullParserException e1) {
    		// TODO Auto-generated catch block
    		e1.printStackTrace();
    	} catch (IOException e1) {
    		// TODO Auto-generated catch block
    		e1.printStackTrace();
    	}

    	poem.addVerse(verse1);
    	poem.addVerse(verse2);

    	poem.setId(id);
    	
    	return poem;
    }

	public List<Poem> search(String string) 
	{

		string=string.replace("ي", "ی");

		List<Poem> poemList = new Vector<IPoemDb.Poem>(2); 

		XmlResourceParser asd = context.getResources().getXml(R.xml.poemdb);

		
		String MesraNo ="" ;
		boolean stringContain=false;

		Poem poem = new Poem(IPoemDb.PoemType.robaei);
		Verse verse1 = new Verse();
		Verse verse2 = new Verse();
		int poemId=-1;

		try 
		{
			while (asd.next() != XmlResourceParser.END_DOCUMENT ) 
			{
				String att = asd.getAttributeValue(null,"Id");
				if (att != null)
				{
					poemId = Integer.valueOf(att);
				}


				String s = asd.getText();
				if	(MesraNo != null)
				{

					if (s != null && MesraNo.equals("1") ) 
					{
						verse1.setPart1(s);
						if (s.contains(string))
							stringContain = true;
					}
					else if (s!=null && MesraNo.equals("2")) 
					{
						verse1.setPart2(s);        			
						if (s.contains(string))
							stringContain = true;
					} 
					else if (s!=null && MesraNo.equals("3")) 
					{
						verse2.setPart1(s);        			
						if (s.contains(string))
							stringContain = true;
					} 
					else if (s!=null && MesraNo.equals("4")) 
					{
						verse2.setPart2(s); 
						if (s.contains(string))
							stringContain = true;

						if (stringContain == true)
						{
							poem.addVerse(verse1);
							poem.addVerse(verse2);
							poem.setId(poemId);
							poemList.add(poem);
							
							verse1= new Verse();
							verse2=new Verse();
							poem = new Poem(IPoemDb.PoemType.robaei);
							
							stringContain=false;

						}		
					} 
				}
				MesraNo = asd.getAttributeValue(null, "No");
			}
		} catch (XmlPullParserException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return poemList;
	}

	public String getPoemBody(int position) {
		// TODO Auto-generated method stub
		return null;
	}
}